import logging
from qiniu import Auth, put_data


access_key = 'yV4GmNBLOgQK-1Sn3o4jktGLFdFSrlywR2C-hvsW'
secret_key = 'bixMURPL6tHjrb8QKVg2tm7n9k8C7vaOeQ4MEoeW'
bucket_name = 'ihome'


def storage(data):
    """七牛云存储上传文件接口"""
    if not data:
        return None
    try:
        q = Auth(access_key, secret_key)
        token = q.upload_token(bucket_name)
        ret, info = put_data(token, None, data)
    except Exception as e:
        # logging.error(e)
        raise e
    # print(ret, info)
    if info and info.status_code != 200:
        raise Exception("上传图片失败")
    # print("=============================")
    # print(ret['key'])
    return ret["key"]


if __name__ == '__main__':
    file_name = input("输入上传的文件")
    with open(file_name, "rb") as f:
        storage(f.read())